package com.manager.traffic.controller;

import com.manager.common.constant.TcpDataType;
import com.manager.common.customer.utils.RemoteExecute;
import com.manager.common.utils.DataTools;
import com.manager.common.vo.PageVO;
import com.manager.common.vo.ResultVO;
import com.manager.model.Mac;
import com.manager.model.SensitiveWord;
import com.manager.traffic.service.MacService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/mac")
public class MacController {

    @Autowired
    private MacService macService;

    @RequestMapping("/getMacPage")
    public PageVO getMacPage(Mac model) {
        return macService.geMacPage(model);
    }

    @RequestMapping("/addOrUpdate")
    public ResultVO addOrUpdate(Mac model) {
        try{
            if(StringUtils.isEmpty(model.getId())){
                macService.addMac(model);
            }else {
                macService.updateMac(model);
            }
            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/delete")
    public ResultVO delete(Mac model) {
        try {
            macService.deleteMac(model);
            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/updateMacEnabled")
    public ResultVO updateMacEnabled(Mac model) {
        try {
            macService.updateMacEnabled(model);
            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/issuedMacList")
    public ResultVO issuedMacList(){
        try {
            List<Mac> list = macService.getMacList();
            list = list == null ? new ArrayList<>() : list;
            //vords
            String url = DataTools.getSystemSettingStringValByKey("issue_instructions_url");
            JSONObject request = new JSONObject();
            request.put("code", TcpDataType.MAC_ADDRESS.getCode());

            JSONObject params = new JSONObject();
            JSONArray macs = new JSONArray();
            for(Mac mac : list){
                JSONObject macJson = new JSONObject();
                macJson.put("mac", mac.getMac());
                macs.add(macJson);
            }
            String serialNumber = DataTools.getSerialNumber();

            params.put("serialNumber", serialNumber);
            params.put("macs", macs);
            request.put("content",params);
            RemoteExecute.executeTcpInstruction(url, request.toString());

            DataTools.putWebSessionId(serialNumber);

            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }
}
